package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive {
    public static void main(String[] args) throws IOException {
        //创建DatagramSocket对象
        DatagramSocket socket = new DatagramSocket(9999);
        //创建DatagramPacket对象
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        //接收数据
        System.out.println("等待发送端发送");
        socket.receive(packet);
        System.out.println("接收完毕");

        //拆包裹
        byte[] data = packet.getData();
        String msg = new String(data,0,packet.getLength());
        String hostName = packet.getAddress().getHostName();
        System.out.println(hostName+"发送了："+msg);
        //释放资源
        socket.close();
    }
}
